using System;
using System.Collections;
using System.Xml;
using TextParsing.GraphStructure;

namespace TextParsing.GraphStructure
{
	class Xml2File
	{
		public static void WriteXml(Document doc,string filename,System.Text.Encoding encoding)
		{
			XmlTextWriter writer = null;
			
			try
			{
        writer = new XmlTextWriter(filename,encoding);
				writer.Formatting = System.Xml.Formatting.Indented;
				writer.WriteStartDocument();

				writer.WriteStartElement(Grammar.Document.DocumentElement);

				writer.WriteElementString(Grammar.Document.FileNameElement,doc.FileName);
				writer.WriteElementString(Grammar.Document.TotalTermCountElement,doc.TotalTermCount.ToString());
				writer.WriteElementString(Grammar.Document.UniqueTermCountElement,doc.UniqueTermCount.ToString());
				writer.WriteElementString(Grammar.Document.TotalSentenceCountElement,doc.TotalSentenceCount.ToString());

				WriteNodes(writer,doc.Nodes);

				writer.WriteEndElement();

				writer.WriteEndDocument();
			}
			catch(Exception ex)
			{
				throw ex;
			}
			finally
			{
				writer.Close();
			}
		}

		static void WriteNodes(XmlTextWriter writer,ICollection nodes)
		{
      IEnumerator nodeEnum = nodes.GetEnumerator();
			while (nodeEnum.MoveNext())
			{
				Node node = (Node) nodeEnum.Current;

				writer.WriteStartElement(Grammar.Node.NodeElement);
				  writer.WriteElementString(Grammar.Node.IdElement,node.Id.ToString());
					writer.WriteElementString(Grammar.Node.TermElement,node.Term);
					writer.WriteElementString(Grammar.Node.TotalTermFrequencyElement,node.TotalTermFrequency.ToString());
          WriteSections(writer,node.Sections);
				writer.WriteEndElement();
			}
		}

		static void WriteSections(XmlTextWriter writer,ICollection sections)
		{
			IEnumerator sectionEnum = sections.GetEnumerator();
			while (sectionEnum.MoveNext())
			{
				Section section = (Section) sectionEnum.Current;

				writer.WriteStartElement(Grammar.Section.SectionElement);
				writer.WriteAttributeString(Grammar.SectionType.SectionTypeAttribute,GetSectionTypeAttribute(section.Type));
				writer.WriteElementString(Grammar.Section.TermFrequencyElement,section.TermFrequency.ToString());
        WriteSentences(writer,section.Sentences);
				writer.WriteEndElement();
			}
		}

		static string GetSectionTypeAttribute(Section.SectionType sectionType) 
		{
			switch (sectionType)
			{
				case Section.SectionType.Heading : return Grammar.SectionType.HeadingTypeAttribute;
				case Section.SectionType.Title : return Grammar.SectionType.TitleTypeAttribute;
				case Section.SectionType.Caption : return Grammar.SectionType.CaptionTypeAttribute;
				case Section.SectionType.Text : return Grammar.SectionType.TextTypeAttribute;
				case Section.SectionType.Link : return Grammar.SectionType.LinkTypeAttribute;
				case Section.SectionType.MetaDescription : return Grammar.SectionType.MetaDescriptionTypeAttribute;
				case Section.SectionType.MetaKeyword : return Grammar.SectionType.MataKeywordTypeAttribute;
			}
			return "";
		}

		static void WriteSentences(XmlTextWriter writer,ICollection sentences)
		{
			IEnumerator senEnum = sentences.GetEnumerator();
			while (senEnum.MoveNext())
			{
				Sentence sen = (Sentence) senEnum.Current;

				writer.WriteStartElement(Grammar.Sentence.SentenceElement);
				writer.WriteElementString(Grammar.Sentence.NumberElement,sen.Number.ToString());
				writer.WriteElementString(Grammar.Sentence.PlaceElement,sen.Place.ToString());

				if (sen.ConnectingWord != null) 
				{
					writer.WriteElementString(Grammar.Sentence.ConnectingWordElement,sen.ConnectingWord);
				}
				if (sen.NextId >= 0) writer.WriteElementString(Grammar.Sentence.NextIdElement,sen.NextId.ToString());

				writer.WriteEndElement();
			}
		}
	}
}
